﻿using CommonTool;
using Motions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Tool;

using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace AutoMachine
{
  public  class Device
    {

        #region 系统配置读取
        //读取配置
        public static bool ConfigAll()
        {
            try
            {
                Frm_Welcome.Instance.Show();//欢迎界面
                Frm_Welcome.Instance.UpdateStep(5, "初始化");

                Thread.Sleep(500);
                //配置轴参数 轴名称
                SystemCfg.WriteAxisNameXmlToSystem(SystemCfg.AxisName, "Axis");                      
                Frm_Welcome.Instance.UpdateStep(10, "配置轴名称参数");

                Thread.Sleep(500);
                //把XML中的轴参数加载到内存中
                //配置 输入 输出 参数
                SystemCfg.WriteCardXmlToSystem(SystemCfg.In, "IN");
                SystemCfg.WriteCardXmlToSystem(SystemCfg.Out, "OUT");          
                Frm_Welcome.Instance.UpdateStep(20, "配置输入输出IO参数");

                //把轴参数参数加载到内存中           
                SystemCfg.WriteAxisCfgXmlToSystem();
                Frm_Welcome.Instance.UpdateStep(30, "配置轴参数");

                //把点位参数              
                SystemCfg.WritePointXmlToSystem(SystemCfg.UpAndDownLoad, "UpAndDownLoad");
                SystemCfg.WritePointXmlToSystem(SystemCfg.MagSplit, "MagSplit");
                SystemCfg.WritePointXmlToSystem(SystemCfg.SlidesTransit, "SlidesTransit");
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarryLocation, "TransitCarryLocation");
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitPickUp, "TransitPickUp");
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming1, "FlyFilming1");
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarry, "TransitCarry");
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming2, "FlyFilming2");
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming3, "FlyFilming3");
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyAfterPickUp, "FlyAfterPickUp");
                SystemCfg.WritePointXmlToSystem(SystemCfg.PutDownOKNG, "PutDownOKNG");
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKFull, "OKFull");
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKMagMerge, "OKMagMerge");


                Frm_Welcome.Instance.UpdateStep(40, "配置点位参数");

                //系统参数
                SystemCfg.WriteParamXmlToSystem();
                Frm_Welcome.Instance.UpdateStep(50, "配置系统参数");
                Thread.Sleep(500);

                //把Ini的配置参数写进内存中
                 SysConfig.WriteIniToSystem();
                Frm_Welcome.Instance.UpdateStep(60, "配置Ini参数");

                Thread.Sleep(500);
              
                //把Ini的生产数据写进内存中
                Count.LoadCount();
                Frm_Welcome.Instance.UpdateStep(70, "配置生产数据");

                Thread.Sleep(500);
                Frm_Welcome.Instance.UpdateStep(80, "参数配置完成");

                //站位添加
                SystemCfg.AddStation();
                Thread.Sleep(500);
                Frm_Welcome.Instance.UpdateStep(90, "准备界面中");

                //加载视觉部分
                //VppConfig.LoadVppConfig();

                //VppConfig.LoadVppCamera();

                //VppConfig.LoadVppTool();
            

                Thread.Sleep(1000);
                Frm_Welcome.Instance.Hide();

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("系统初始化错误" + ex.Message);
            }
            return false;
        }

        #endregion

        #region 初始化

        /// <summary>
        /// 初始化硬件
        /// </summary>
        public static void InitSyetem()
        {
            Action<object> action = (object obj) =>
            {
                //延迟一秒初始化硬件，便于窗口接收初始化异常
                Task.Delay(1000);
                //初始化轴卡
                MotionMgr.GetInstance().InitMotionCard();
                
                //初始化扩展IO板卡
              //  MotionMgr.GetInstance().InitExtIoCard();

                //系统管理器线程，负责清除过期文件数据，检查系统是否空闲无操作
                SystemMgr.GetInstance().StartMonitor();

                //如果需要程序一启动就给轴上电，可以在此处添加代码
                //todo: tcp, com, vision 
            };
            Task t1 = new Task(action, "");
            t1.Start();

        }

        #endregion

        #region 反初始化
        public static void DeinitSystem()
        {
            //反初始化轴卡
            MotionMgr.GetInstance().DeinitMotionCard();

            SystemMgr.GetInstance().StopMonitor();

        }

        #endregion

        #region  设备运行模式


        private static OperationMode _opermode = OperationMode.NormalMode;//初始运行模式
        public static OperationMode OperMode
        {

            get
            {
                return _opermode;
            }
        }


        public enum OperationMode
        {
            /// <summary>
            /// 正常运行模式
            /// </summary>
            NormalMode = 0,//正常运行模式

            DryRunMode = 1,//空跑模式
        }

        public delegate void OperationModeChangedHandler(OperationMode OperMode);

        public static event OperationModeChangedHandler OperationModeChangeEvent;

        public static void ChangeOperationMode(OperationMode OperMode)
        {
            if (_opermode == OperMode)
            {
                return;
            }
            _opermode = OperMode;
            if (OperationModeChangeEvent != null)
            {
                OperationModeChangeEvent(OperMode);
            }

        }


        #endregion

        #region 设备操作等级

        private static OperationLevel _operlevel = OperationLevel.NoLevel;//初始等级
        public static OperationLevel OperLevel
        {

            get
            {
                return _operlevel;
            }
        }


        public enum OperationLevel
        {
            Operator,//操作员

            NoLevel,//未登录，默认未最低权限

            Admin,//管理员

            Developer,//开发人员

          
        }

        public delegate void OperationLevelChangedHandler(OperationLevel OperLevel);

        public static event OperationLevelChangedHandler OperationLevelChangeEvent;

        public static void ChangeOperationLevel(OperationLevel OperLevel)
        {
            if (_operlevel == OperLevel)
            {
                return;
            }
            _operlevel = OperLevel;
            if (OperationLevelChangeEvent != null)
            {
                OperationLevelChangeEvent(OperLevel);
            }

        }


        #endregion

        #region 设备状态模式

        private static STATEMode _statemode = STATEMode.STOP;//初始状态模式
        public static STATEMode StateMode
        {

            get
            {
                return _statemode;
            }
        
        }


        public enum STATEMode
        {       
            /// <summary>
            /// 设备运行中
            /// </summary>
            RUN,
            /// <summary>
            /// 设备暂停中
            /// </summary>
            PAUSE,
            /// <summary>
            /// 设备停止中
            /// </summary>
            STOP,
            /// <summary>
            /// 设备急停中
            /// </summary>
            EMG,
            /// <summary>
            /// 设备报警中
            /// </summary>
            ALARM,
            /// <summary>
            /// 设备复位完成
            /// </summary>
            Reseted,
            /// <summary>
            /// 设备正在复位
            /// </summary>
            Resetting,
        }

        public delegate void StateModeChangedHandler(STATEMode StateMode);

        public static event StateModeChangedHandler StateModeChangeEvent;

        public static void ChangeStateMode(STATEMode StateMode)
        {
            if (_statemode == StateMode)
            {
                return;
            }
            _statemode = StateMode;
            if (StateModeChangeEvent != null)
            {
                StateModeChangeEvent(StateMode);
            }

        }



        #endregion


    }
}
